#!/usr/bin/python3
# coding=utf-8
import json

import bottle
from bottle import get, run, post, request, route
from util.configUtil import sysConfig

from pythonManger.util.DataRespUtil import errorBack, successBack, callBack
from pythonManger.util.MySqlUtil import addCrashModel, addCrashTypeModel, findCrashByPage, findCrashSize, \
  findCrashTypeByPage, findCrashTypeSize, delFun


@get('/')
def index():
  sub_str = "你好"
  return sub_str


@post('/user/login')
def login():
  sub_obj = {"code": "000000", "data": {"token": "admin-token"}}
  return sub_obj


@get('/user/info')
def userInfo():
  sub_obj = {"code": "000000", "data": {"roles": ["admin"], "introduction": "I am a super administrator",
                                     "avatar": "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
                                     "name": "Super Admin"}}
  return sub_obj


# {
#
#   "name": "测1试",
#   "deleted":0,
#   "des":"初始化测试",
#   "type_id": 0,
#   "attach":"无"
# }
@post('/py/saveCrash')
def saveCrashModel():
  data = request.json
  try:
    addCrashModel(data)
    return successBack('保存成功')
  except Exception as e:
    return errorBack('异常了' + e)


@get('/py/findCrash')
def findCrashFun():
  data = request.params
  # name 异常名称
  # creat_time 创建时间
  # deleted 是否已删除 0、未删 1、已删
  # des 备注
  name =request.query.name
  # print('name', name)
  try:
    res = findCrashByPage(data['index'], data['size'],name)
    pages = findCrashSize(name)[0][0]
    result = {
      'crashArr': [],
      'pages': pages
    }

    for item in res:
      obj = {
        "id": item[0],
        "name": item[1],
        "creat_time": str(item[2]),
        "update_time": str(item[3]),
        "deleted": item[4],
        "des": item[5],
        "type_id": item[6],
        "attach": item[7]
      }
      result['crashArr'].append(obj)
    return callBack('000000', result, "查询成功")
  except Exception as e:
    return errorBack('异常了' + e)



@get('/py/findCrashType')
def findCrashTypeFun():
  data = request.params
  # name 异常名称
  # creat_time 创建时间
  # deleted 是否已删除 0、未删 1、已删
  # des 备注
  name =request.query.name
  try:
    res = findCrashTypeByPage(data['index'], data['size'],name)
    pages = findCrashTypeSize(name)[0][0]
    result = {
      'crashArrType': [],
      'pages': pages
    }

    for item in res:
      obj = {
        "id": item[0],
        "name": item[1],
        "creat_time": str(item[2]),
        "deleted": item[3],
        "des": item[4]
      }
      result['crashArrType'].append(obj)
    return callBack('000000', result, "查询成功")
  except Exception as e:
    return errorBack('异常了' + e)

@route('/py/saveCrashType', method='POST')
def saveCrashTypeModel():
  data = request.params.decode('utf-8')
  # print(data['name'])
  try:
    addCrashTypeModel(data)
    return successBack('保存成功')
  except Exception as e:
    return errorBack('异常了' + e)


@route('/py/del', method='POST')
def saveCrashTypeModel():
  data = request.params
  try:
    delFun(data)
    return successBack('保存成功')
  except Exception as e:
    return errorBack('异常了' + e)

run(host=sysConfig()['baseUrl'], port=sysConfig()['port'])
